//
//  WBWelcomeView.swift
//  SwiftWeibo
//
//  Created by Lausen on 2017/4/21.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcomeView: UIView {
    
    /// 这个是头像的图片视图
    @IBOutlet weak var iconImageView: UIImageView!
    /// 这个是label的视图
    @IBOutlet weak var backLabel: UILabel!
    /// 这个是头像底部的约束
    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    
    class func welcomeView() -> WBWelcomeView {
        
        let nib = UINib.init(nibName: "WBWelcomeView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        
        v.frame = UIScreen.main.bounds
        
        return v
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        //提示:initWithCoder:只是刚刚从xib的二进制文件将视图数据加载完成
        //还没有和代码连线建立关系,所以开发时,千万不要在这个方法中处理UI
        
    }
    
    override func awakeFromNib() {
        
        // 加载头像视图
        guard let urlString = WBNetworkManager.shared.userAccount.avatar_large,
            let url = URL(string: urlString) else {
                
                return
        }
        
        iconImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
    
    }
    
    /// 这个是视图已经加载到了window上之后,调用的方法
    override func didMoveToWindow() {
        
        //视图出现在window上是,布局还没有完成,这个时候,所有的frame都要有动画效果,为了解决这个现象,需要在次调用
        layoutIfNeeded()
        
        bottomCons.constant = bounds.height - 200.0
        
        UIView.animate(
            withDuration: 2.0,              //动画时长
            delay: 0,                       //延时多久
            usingSpringWithDamping: 0.7,    //弹性系数
            initialSpringVelocity: 5,       //初始速度
            options: [],                    //可选项
            animations: {
            
                self.layoutIfNeeded()
                
        }) { (completion) in
            
            UIView.animate(withDuration: 1, animations: { 
                
                self.backLabel.alpha = 1.0
                
            }, completion: { (isCompletion) in
                
                self.removeFromSuperview()
            })
        }
        
    }
}
